package com.qk.management.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.PageResult;
import com.qk.common.enums.ParamEnum;
import com.qk.common.exception.CommonException;
import com.qk.domain.customer.CustomerDO;
import com.qk.dto.customer.CustomerAddDTO;
import com.qk.dto.customer.CustomerDTO;
import com.qk.entity.Customer;
import com.qk.management.mapper.CustomerMapper;
import com.qk.management.service.CustomerService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author 李智勇
 * @data 2025/10/17 21:21
 */
@Service
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer> implements CustomerService {
    @Override
    public void add(CustomerAddDTO dto) {
        if(ObjectUtil.isEmpty(dto.getName())||ObjectUtil.isEmpty(dto.getPhone())){
            CommonException.throwCommonException(ParamEnum.PARAM_ERROR);
        }
        Customer customer = BeanUtil.copyProperties(dto, Customer.class);
        customer.setCreateTime(LocalDateTime.now());
        customer.setUpdateTime(LocalDateTime.now());
        this.save(customer);
    }

    @Override
    public PageResult<CustomerDO> customerList(CustomerDTO dto) {
        PageHelper.startPage(dto.getPage(),dto.getPageSize());
        List<CustomerDO> list = baseMapper.selectByPage(dto);
        Page<CustomerDO> page = (Page<CustomerDO>) list;
        return PageResult.<CustomerDO>builder()
                .total(page.getTotal())
                .rows(page.getResult())
                .build();

    }
}
